package com.whut.monitor.controller;

import com.whut.monitor.netty.FileTransferClient;
import com.whut.monitor.netty.FileTransferServer;
import com.whut.monitor.vo.ResponseMsg;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.UUID;

/**
 * Created by zll on 2018/7/14 0014.
 * 文件传输
 */

@Controller
public class FileTransferController {

    @RequestMapping("/fileTransfer")
    @ResponseBody
    public ResponseMsg fileTransfer(@RequestParam("file")MultipartFile file) throws IOException {

 //      (File) file
        File tempt = new File("F://"+file.getOriginalFilename());
        file.transferTo(tempt);

        ResponseMsg j = new ResponseMsg();
        try {
            FileTransferServer server = new FileTransferServer(); // 启动服务端
            FileTransferClient client = new FileTransferClient(); // 启动客户端连接
            client.sendFile(tempt); // 传输文件
            server.load();
            j.setMsg("文件发送成功！");
            j.setSuccess(true);
        } catch (Exception e) {
            e.printStackTrace();
            j.setMsg(e.getMessage());
        }finally{
            if( tempt.exists()){
                tempt.delete();
            }
        }
        return j;

    }
}
